*******************************************************************************
*****************************  Figure A5  *************************************
*****  Tracing the source of disempowerment: Fraction of total votes for ******
************  men and women, by source and across conditions  *****************
*******************************************************************************

clear all
set more off

//cd "C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable/figure_a5"

*******************************************************************************

use "figure_a5_dataset.dta", replace

collapse (mean) *val *exp, by(female condition_maj_min)
reshape long @_val @_exp, i(female condition) j(variable) string

g mean = .
g sd = .
g N = .
g group_N = .
g pXexp = .

forval i = 1/`c(N)' {
local f = female[`i']
local c = condition_maj_min[`i']
local v = variable[`i']
local exp = _exp[`i']

preserve
use "figure_a5_dataset.dta", clear
sum `v'_val if female == `f' & condition_maj_min==`c', d
local xbar = `r(mean)'
local sd = `r(sd)'
local N = `r(N)'
ttest `v'_val = `exp' if female == `f' & condition_maj_min==`c'
local pXexp = `r(p)'
distinct studyXgroup if female == `f' & condition_maj_min==`c'
local group_N = `r(ndistinct)'
restore

replace mean = `xbar' if _n==`i'
replace sd = `sd' if _n==`i'
replace N = `N' if _n==`i'
replace pXexp = `pXexp' if _n==`i'
replace group_N = `group_N' if _n==`i'

}

g meanse = sd/sqrt(N)
g clustermeanse = sd/sqrt(group_N)
g tIID = (_val-_exp)/meanse
g tcluster = (_val-_exp)/ clustermeanse
g ci =  clustermeanse*1.96
g sex = cond(female==1, "Women", "Men")
g pval_cluster = (2 * ttail(group_N-1, abs((_val-_exp)/clustermeanse)))
export delimited using "./figure_a5_input.csv", delim(",") replace
